package com.ning.run.web.controller;

import com.ning.run.POJO.PO.Memberinfo;
import com.ning.run.POJO.TO.MemberInfoFindPassTO;
import com.ning.run.POJO.TO.MemberInfoLoginTO;
import com.ning.run.POJO.TO.MemberInfoRegisterTO;
import com.ning.run.POJO.VO.LoginVO;
import com.ning.run.common.utils.IpUtil;
import com.ning.run.service.MemberinfoService;
import com.ning.run.web.controller.response.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @Description:
 * @Author:ning
 * @Email:cxy_ning@163.com
 * @Date:2022/11/20 20:13
 **/
@RestController
@RequestMapping("/member_info")
public class MemberInfoController {
    @Autowired
    MemberinfoService memberinfoService;

    @PostMapping("/register")
    public R Register(@RequestBody MemberInfoRegisterTO registerTO){
        try{
            memberinfoService.register(registerTO);
        }catch (Exception e){
            return R.failure(e.getMessage());
        }
        return R.success("注册成功!!!");
    }
    @PostMapping("/login")
    public R Login(@RequestBody MemberInfoLoginTO loginTO){
        Memberinfo memberinfo=null;
        try{
            memberinfo = memberinfoService.login(loginTO);
        }catch (Exception e){
            return R.failure(e.getMessage());
        }
        return R.success(memberinfo);
    }
    @PostMapping("/logout")
    public R Logout(@RequestBody int userId){
        //后面优化 用redis
        try{
            System.out.println(userId);
            memberinfoService.logout(userId);
        }catch (Exception e){
            return R.failure(e.getMessage());
        }
        return R.success("退出成功!!!");
    }
    @GetMapping("/login_data")
    public R loginData(HttpServletRequest request){
        LoginVO loginVO = memberinfoService.loginData();

        //真实ip地址
        loginVO.setRealIp(IpUtil.getReadAdd(IpUtil.getIpAddr(request)));

        return R.success(loginVO);
    }

    @PostMapping("/find_account")
    public R findAccount(@RequestBody MemberInfoFindPassTO memberInfoFindPassTO){
        try{
            memberinfoService.findAccount(memberInfoFindPassTO);
        }catch (Exception e){
            e.printStackTrace();
        }
        return R.success();
    }
}
